package com.ruoyi.cms.controller;

import com.ruoyi.cms.domain.CmsLoopAction;
import com.ruoyi.cms.service.ICmsLoopActionService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 轮播广告Controller
 *
 * @author houyong
 * @date 2025-11-08
 */
@Tag(name = "CmsLoopActionController", description = "轮播广告管理for管理后台")
@RestController
@RequestMapping("cms/manager/action")
public class CmsLoopActionController extends BaseController {
    @Autowired
    private ICmsLoopActionService cmsLoopActionService;

    /**
     * 查询轮播广告列表
     */
//    @RequiresPermissions("system:action:list")
    @GetMapping("/list")
    public TableDataInfo list(CmsLoopAction cmsLoopAction) {
        startPage();
        List<CmsLoopAction> list = cmsLoopActionService.selectCmsLoopActionList(cmsLoopAction);
        return getDataTable(list);
    }

    /**
     * 导出轮播广告列表
     */
//    @RequiresPermissions("system:action:export")
    @Log(title = "导出轮播广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsLoopAction cmsLoopAction) {
        List<CmsLoopAction> list = cmsLoopActionService.selectCmsLoopActionList(cmsLoopAction);
        ExcelUtil<CmsLoopAction> util = new ExcelUtil<CmsLoopAction>(CmsLoopAction.class);
        util.exportExcel(response, list, "轮播广告数据");
    }

    /**
     * 获取轮播广告详细信息
     */
//    @RequiresPermissions("system:action:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cmsLoopActionService.selectCmsLoopActionById(id));
    }

    /**
     * 新增轮播广告
     */
//    @RequiresPermissions("system:action:add")
    @Log(title = "新增轮播广告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsLoopAction cmsLoopAction) {
        cmsLoopAction.setStatus(1L);
        cmsLoopAction.setCreateTime(new Date());
        cmsLoopAction.setCreateBy(SecurityUtils.getUsername());
        return toAjax(cmsLoopActionService.insertCmsLoopAction(cmsLoopAction));
    }

    /**
     * 修改轮播广告
     */
//    @RequiresPermissions("system:action:edit")
    @Log(title = "编辑轮播广告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsLoopAction cmsLoopAction) {
        cmsLoopAction.setUpdateTime(new Date());
        cmsLoopAction.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(cmsLoopActionService.updateCmsLoopAction(cmsLoopAction));
    }

    /**
     * 删除轮播广告
     */
//    @RequiresPermissions("system:action:remove")
    @Log(title = "删除轮播广告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmsLoopActionService.deleteCmsLoopActionByIds(ids));
    }

    //TODO 更新轮播广告的上线状态
    @Log(title = "更新轮播广告状态", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody CmsLoopAction cmsLoopAction) {
        return toAjax(cmsLoopActionService.updateStatus(cmsLoopAction));
    }


    @Log(title = "轮播广告更新排序", businessType = BusinessType.UPDATE)
    @PutMapping("/sort")
    public AjaxResult updateSortBatch(@RequestBody List<CmsLoopAction> loopActions) {
        return toAjax(cmsLoopActionService.updateSortBatch(loopActions));
    }


    @Log(title = "批量删除轮播广告", businessType = BusinessType.DELETE)
    @PostMapping("/batch/delete")
    public AjaxResult batchDelete(@RequestBody Long[] ids) {
        return toAjax(cmsLoopActionService.deleteCmsLoopActionByIds(ids));
    }
}
